package lq.book.service;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lq.book.bean.User;

public class SessionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public SessionServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	    this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
  {
		 BookService bs=new BookService(); 
		 String username=request.getParameter("username");
		 String password=request.getParameter("password");
		 System.out.println(username+password);
		 Integer i=bs.checkUser(username, password);
		 System.out.println(i);
		 HttpSession session=request.getSession();
		  
		  User user=new User();
		 // System.out.println(request.getParameter("name")+request.getParameter("pass"));
		  
		
		 if(i!=0)
              { 
        	  
        	       user.setUsername(username);
        	       user.setId(i);      	      
        	       session.setAttribute("user",user);
        	    if(request.getParameter("auto")!=null&&request.getParameter("auto").equals("on"))
        	   {
        		   Cookie name=new Cookie("name",username );
        		   Cookie pass=new Cookie("pass",password );
        		   name.setMaxAge(60*60*24*7);
        		   pass.setMaxAge(60*60*24*7);
        		   response.addCookie(name);
        		   response.addCookie(pass);
        		  
        	   }
        	   response.sendRedirect("ShowBook");
           }
         else
         {
        	 
        	 response.sendRedirect("Login.jsp?check=false");
        	 
         }
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException 
	
	{
		// Put your code here
	}

}
